<?php
/**
 * Created by PhpStorm.
 * User: ShaDoW
 * Date: 29.11.13
 * Time: 15:52
 */

class SendMail {

	public static function send($email,$mail_type,$data) {
		$tpl = MailTemplate::model()->findByAttributes(array('name'=>$mail_type));
		$body = $tpl->body;
		if( is_array($data) && !empty($data) ) {
			foreach( $data as $key => $value ) {
				$body = str_replace('{'.$key.'}',$value,$body);
			}
		}

		$title = $tpl->title;
		$mailer = Yii::createComponent('application.extensions.mailer.EMailer');
		//$mailer->Host = 'mx1.mirohost.net';//"mx1.mirohost.net";
		$mailer->Host = 'mail.instinct.kz';//"mx1.mirohost.net";
		$mailer->IsSMTP();
		$mailer->SMTPDebug = 1;
		$mailer->CharSet = 'UTF-8';
		$mailer->IsHTML(true);
		$mailer->SMTPAuth = true;
		$mailer->Timeout = 60;
		//$mailer->SMTPSecure = '';
		$mailer->Port = 25;//25;
//		$mailer->Username = 'info@igroteka.com.ua';//"info@igroteka.com.ua";
		$mailer->Username = 'service.info@instinct.kz';//"info@igroteka.com.ua";
		$mailer->Password = '!Q2w3e4r';//"UHqpfz4D";
		$mailer->setFrom('service.info@instinct.kz','Сервис доставки');
		$mailer->AddAddress($email);
		$mailer->Subject = $title;
		//$mailer->Body = $body;
		$server_name = Yii::app()->request->getServerName();
		if( trim($server_name) == 'service' ) {
			$server_name = 'igroteka.com.ua';
		}
		$server_name = 'http://'.$server_name;
		$css =
			'<style type="text/css">
				p { color: rgba(0, 0, 0, 0.73); font-size: 14px; font-family: sans-serif; margin: 3px 0 0; padding: 0; }
				a:hover, a:visited, a { color: #3695CB; }
			</style>';
		$mailer->MsgHTML($css.$body,$server_name);
		if( !$mailer->Send() ) {
			$error = 'Mail error: '.$mailer->ErrorInfo;
			Yii2Debug::dump($error);
			return false;
		} else {
			return true;
		}
	}
} 